package com.gjintel.framework.taglibs;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.log4j.Logger;

@SuppressWarnings("serial")
public class PostTreeTag extends BodyTagSupport {
	private static final Logger LOG = Logger.getLogger(PostTreeTag.class);
	private String ctx; // 项目路径
	private String postIds;
	private String postNames;
	private String selectMethod;

	private enum selectMethodValue {
		SINGLE, MULTITERM
	}

	@Override
	public int doEndTag() throws JspException {
		try {
			StringBuilder bfString = new StringBuilder();
			bfString.append("<div id='").append(postIds).append("Tool' style='padding:5px;display: none;'><div style='margin-bottom:5px'>节点名称<input type='text' id='sel").append(
					postIds).append("PostTreeName' name='treeName'/>&nbsp;&nbsp;<a href='javascript:void(0);' onclick='sel").append(postIds).append(
					"PostTree();' class='easyui-linkbutton' >查询</a></div></div>");
			bfString.append("<div id=\"").append(postIds).append("PostTreeDialog").append(
					"\" class=\"easyui-dialog\" title=\"职务选择树\" style=\"width:320px;height:442px;padding:8px;padding-top: 0px;\"" + "data-options=\"" + "iconCls: 'icon-post',"
							+ "method:'post'," + "closed:true,modal: true," + "toolbar:'#").append(postIds).append(
					"Tool'," + "buttons: [{" + "	text:'选择'," + "	iconCls:'icon-ok'," + "	handler:function(){");
			if (this.selectMethod.trim().equalsIgnoreCase(PostTreeTag.selectMethodValue.SINGLE.toString())) {
				bfString.append("getSelect").append(postIds).append("PostNode();");
			} else {
				bfString.append("getSelects").append(postIds).append("PostNode();");
			}
			bfString.append("	}" + "},{" + "	text:'取消'," + "	iconCls:'icon-cancel'," + "	handler:function(){" + "		$('#").append(postIds).append("PostTreeDialog").append(
					"').dialog('close');" + "	}" + "}]" + "\">" + "<ul id=\"").append(postIds).append(
					"PostTree\" class=\"easyui-tree\" data-options=\"url:'',method:'get',animate:true,");
			if (this.selectMethod.trim().equalsIgnoreCase(PostTreeTag.selectMethodValue.MULTITERM.toString())) {
				bfString.append("checkbox:true,");
			}
			bfString.append(
					"loadFilter:function(data){ids=data.selIds;return data.tree;}," + "formatter:function(node){" + "	if(ids != undefined){" + "		for(var i=0;i<ids.length;i++){"
							+ "			if(node.id == ids[i]){" + "				return '<span style=\\'color:red;font-weight: bold;\\'>'+node.text+'</span>';" + "			}" + "		}" + "	}"
							+ "	return node.text;" + "}\"></ul>" + "</div>" + "<script type=\"text/javascript\">" + "var ids;" + "function getSelect").append(postIds).append(
					"PostNode(){" + "	var node = $('#").append(postIds).append(
					"PostTree').tree('getSelected');" + "	if(node == null){" + "		$.messager.alert('提示','请选择职务节点！','info');" + "	}else{" + "		$('#" + postNames
							+ "').val(node.text); $('#" + postNames + "').focus();" + "		$('#" + postIds + "').val(node.id);" + "		$('#").append(postIds).append("PostTreeDialog")
					.append("').dialog('close');}" + "}" + "function getSelects").append(postIds).append("PostNode(){" + "	var nodes = $('#").append(postIds).append(
							"PostTree').tree('getChecked');" + "	var nodeNames = '';" + "	var nodeIds = '';" + "	for(var i=0; i<nodes.length; i++){"
									+ "		if (nodeNames != '') nodeNames += ',';" + "		nodeNames += nodes[i].text;" + "		if (nodeIds != '') nodeIds += ',';"
									+ "		nodeIds += nodes[i].id;" + "	}" + "	$('#" + postNames + "').val(nodeNames);" + "	$('#" + postIds + "').val(nodeIds);" + "	$('#").append(
							postIds).append("PostTreeDialog").append("').dialog('close');" + "}" + "function sel").append(postIds).append("PostTree(selectIds){" + "$('#").append(
							postIds).append("PostTree').tree('options').url='" + ctx + "/framework/post/getPostTreeDialog?nodeName='+encodeURI(encodeURI($('#sel").append(postIds)
					.append("PostTreeName').val()))+'& deleteIds ='+selectIds;");
			// 对于单选树回显处理
			if (this.selectMethod.trim().equalsIgnoreCase(PostTreeTag.selectMethodValue.SINGLE.toString())) {
				bfString.append("$('#").append(postIds).append("PostTree').tree({onLoadSuccess:function(node, data){" + "var node = $('#").append(postIds).append(
						"PostTree').tree('find', selectIds);" + "if(node){" + "$('#").append(postIds).append(
						"PostTree').tree('expand', node.target).tree('select', node.target);" + "}" + "}})");
			}
			bfString.append("}" + "</script>");
			pageContext.getOut().print(bfString.toString());
		} catch (IOException e) {
			LOG.debug("获得职务树出现异常！", e);
		}
		return super.doEndTag();
	}

	public String getCtx() {
		return ctx;
	}

	public void setCtx(String ctx) {
		this.ctx = ctx;
	}

	public String getPostIds() {
		return postIds;
	}

	public void setPostIds(String postIds) {
		this.postIds = postIds;
	}

	public String getPostNames() {
		return postNames;
	}

	public void setPostNames(String postNames) {
		this.postNames = postNames;
	}

	public String getSelectMethod() {
		return selectMethod;
	}

	public void setSelectMethod(String selectMethod) {
		this.selectMethod = selectMethod;
	}
}
